package client;


import bean.User;
import org.apache.commons.beanutils.BeanUtils;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        //2.将页面获取的数据存储为map键值对的格式保存
        Map<String, String[]> map = request.getParameterMap();
        //3.封装User对象
        User user = new User();
        try {//遍历map键值对当中的key对应的value值赋值给Bean熟悉的user
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //4.创建UserService对象，
        UserService service = new UserService();
        //调用service里的修改数据方法
        service.updateUser(user);
        //5.重定向跳转到查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/listUser");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
